<?php
if(empty($_SERVER['SHELL'])){
	exit;   
}

include 'config.php';
include $config['path'].'lib/Eolin.Service.Core.Enhance.php';
include $config['path'].'lib/Builder.php';
include $config['path'].'lib/replaceBase64.php';

$loader = new Builder($config['path'].'monkeyfly.template.js');
foreach($config['replaces'] as $key=>$replace){
	$loader->addReplace('{'.$key.'}', $replace[0], $replace[1]);	
}
$loader->build($config['path'].$config['userscript_filename']);


include $config['path'].'lib/firefoxAddonCompiler.php';

$loader = new Builder($config['path'].'package/chrome/src/manifest.json.template');
foreach($config['replaces'] as $key=>$replace){
	$loader->addReplace('{'.$key.'}', $replace[0], $replace[1]);	
}
$loader->build($config['path'].'package/chrome/src/manifest.json');

/*
Deprecated
$loader = new Builder($config['path'].'monkeyfly.loader.template.js');
foreach($config['replaces'] as $key=>$replace){
	$loader->addReplace('{'.$key.'}', $replace[0], $replace[1]);	
}
$loader->build($config['path'].$config['loader_filename']);

*/

$loader = new Builder($config['path'].'package/chrome/src/chrome_updateInfo.xml.template');
foreach($config['replaces'] as $key=>$replace){
	$loader->addReplace('{'.$key.'}', $replace[0], $replace[1]);
}
$loader->build($config['path'].'package/chrome/bin/'.$config['chrome_updater']);

$base64Convertor = new replaceBase64($config['path']);
$base64Convertor->addFile($config['path'].'twitter.css', $config['path'].'twitter.css');
$base64Convertor->convert();

include_once($config['path'].'/lib/simple_html_dom.php');
$xml = shell_exec('svn info --xml https://monkeyfly.googlecode.com/svn/trunk');
$xparser = new simple_html_dom();
$xparser->load($xml);
$_t = $xparser->find('entry');
$newRevision = $_t[0]->revision;

$db = new DBQueryi(NULL,$config['database']['host'], $config['database']['id'], $config['database']['pwd'], $config['database']['db']);
$lastRevision = $db->queryCell("SELECT revision FROM {$config['database']['prefix']}_updates ORDER BY id DESC LIMIT 1");

if($lastRevision != $newRevision) {
	$db->query("INSERT INTO {$config['database']['prefix']}_updates (revision,created,log,configuration) VALUES('{$newRevision}', NOW(),'".$db->e(isset($argv[1]) ? $argv[1] : '')."','".$db->e(file_get_contents("{$config['path']}config.php"))	."')");
	$f = fopen($config['path'].'/version.php','w');
	fwrite($f,'<?=$_GET[\'callback\']?>('.$newRevision.')');
	fclose($f);
}

copy("{$config['path']}{$config['userscript_filename']}","{$config['path']}package/chrome/src/{$config['userscript_filename']}");
$zip = "zip -j {$config['path']}{$config['chrome_extension']} {$config['path']}package/chrome/src/manifest.json {$config['path']}{$config['userscript_filename']} {$config['path']}package/chrome/src/icon16.png {$config['path']}package/chrome/src/icon32.png {$config['path']}package/chrome/src/icon48.png {$config['path']}package/chrome/src/icon128.png";
echo $zip;
echo shell_exec($zip);

$cp = "cp {$config['path']}package/chrome/src/manifest.json {$config['path']}{$config['userscript_filename']} {$config['path']}package/chrome/src/icon16.png {$config['path']}package/chrome/src/icon32.png {$config['path']}package/chrome/src/icon48.png {$config['path']}package/chrome/src/icon128.png {$config['path']}package/chrome/src/extension_components";
echo $cp;
echo shell_exec($cp);


?>